import { Widget, _decorator, Node, tween, v3, Label, Vec2, v2, math, Tween } from 'cc';
import { GameMgr } from '../manager/GameMgr';
import { AudioMgr } from '../manager/AudioMgr';
import { CoinMgr } from '../manager/CoinMgr';
import { ArrayUtil } from '../util/ArrayUtil';
import { InputBlock } from '../misc/InputBlock';
import { TimeMgr } from '../manager/TimeMgr';
import { UI } from '../enum/UI';
import { UIBase } from '../scriptBase/UIBase';
import { CoinItem } from '../uiItem/CoinItem';
import { Global } from '../Global';
import { TransMgr } from '../manager/TransMgr';
import { BYTEDANCE } from 'cc/env';
import { ModeName, ModeType } from '../enum/Mode';
import { AdMgr } from '../manager/AdMgr';
import { TimeItem } from '../uiItem/TimeItem';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('UI/UIWin')
@requireComponent(Widget)
export class UIWin extends UIBase {

    @property(Node)
    private succNode: Node = null;

    @property(Node)
    private succNode2: Node = null;

    @property(Node)
    private failNode: Node = null;

    @property(Node)
    private layoutNode: Node = null;

    private pointer: Node = null
    private lbCoin: Label = null
    private coinItem: CoinItem = null

    private coin: number = 0
    public set Coin(v: number) {
        this.coin = Math.max(v, 0)
        this.lbCoin.string = `x${this.coin}`
    }

    private multi: number = 1

    private readonly posArr: Vec2[] = [
        v2(-120, -74),
        v2(-72, 80),
        v2(-17, 29),
        v2(32, 86),
        v2(90, 120),
    ]

    private readonly multiArr: number[] = [2, 3, 5, 3, 4]

    private tw: Tween<Node> = null

    protected onLoad(): void {

    }

    // 1 基础模式成功，2基础模式失败
    public onOpen(data?: unknown): void {
        AudioMgr.stopBgm()
        this.failNode.active = false;
        this.succNode.active = false;
        this.succNode2.active = false;
        if (data == 1){
            AudioMgr.playSfx('通关成功')
            GameMgr.passGame()
            if (GameMgr.gameCampSum >= 2){
                this.succNode2.active = true;
                GameMgr.gameCampWin[GameMgr.gameCampCur]++;

                if (GameMgr.modeType == ModeType.caipingQinglv){
                    this.succNode2.getChildByName("tilLab").getComponent(Label).string = GameMgr.gameCampCur==0?"男方获胜":"女方获胜";
                }else if (GameMgr.modeType == ModeType.caipingDashi){
                    this.succNode2.getChildByName("tilLab").getComponent(Label).string = GameMgr.gameCampCur==0?"红方获胜":"蓝方获胜";
                }
            }else{
                this.succNode.active = true;
            }
        }else{
            AudioMgr.playSfx('通关失败')
            this.failNode.active = true;
        }
        // this.coinItem.init(CoinMgr.CurCoin)

        for (let index = 0; index < this.layoutNode.children.length; index++) {
            const element = this.layoutNode.children[index];
            if(index == 0){
                element.active = false;
            }
            else if (index == 1){
                element.getChildByName("desLab").getComponent(Label).string = "时间：";
                element.getChildByName("valLab").getComponent(Label).string =  Math.floor(GameMgr.useTime)  +  "秒";
            }
            else if (index == 2){
                element.getChildByName("desLab").getComponent(Label).string = "交换次数：";
                element.getChildByName("valLab").getComponent(Label).string =  GameMgr.stepNum +  "次";
            }
            else if (index == 3){
                element.getChildByName("desLab").getComponent(Label).string = "最高猜对数：";
                element.getChildByName("valLab").getComponent(Label).string =  GameMgr.rightMaxNum +  "个";
            }
             else if (index == 4){
                if (GameMgr.modeType == ModeType.caipingChuangguan || GameMgr.modeType == ModeType.caipingWang){
                    element.active = false;
                }else{
                    element.active = true;
                    element.getChildByName("desLab").getComponent(Label).string = "验证次数：";
                    element.getChildByName("valLab").getComponent(Label).string =  GameMgr.sureNum +  "次";
                }
            }
                
        }

        GameMgr.Pause = true

        // const duration: number = 0.75
        // this.tw = tween(this.pointer).sequence(
        //     tween(this.pointer).to(duration, { position: v3(120, -25) }),
        //     tween(this.pointer).to(duration, { position: v3(-120, -25) })
        // ).repeatForever().start()

        // this.Coin = Global.Normal_Level_Coin_Default_Cnt + (GameMgr.CurLevel - 1) * Global.Normal_Level_Coin_Add_Cnt
        // CoinMgr.CurCoin += this.coin

    }

    public onClose(data?: unknown): void {
        GameMgr.Pause = false
        InputBlock.Active = false
    }

    protected async onBtnRestartClick() {
        await TransMgr.Inst.fadeIn()
        GameMgr.startGame()
        this.close()
        TransMgr.Inst.fadeOut()
    }

    protected async onBtnBackClick() {
        await TransMgr.Inst.fadeIn()
        GameMgr.quitGame()
        this.close()
        this.open(UI.Main)
        TransMgr.Inst.fadeOut()
    }

}


